<template>
  <div>
    <hr />
    <el-form
      :inline="true"
      :model="searchData"
      class="demo-form-inline"
      style="margin-top: 20px; margin-bottom: 5px"
    >
      <el-form-item>
        <el-select v-model="searchData.type" placeholder="请选择类型">
          <el-option label="姓名" value="name"></el-option>
          <el-option label="年龄" value="age"></el-option>
          <el-option label="性别" value="gender"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchData.value"
          placeholder="请输入搜索内容"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="searchStudents()">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="students" border style="width: 100%; margin-bottom: 20px">
      <el-table-column prop="_id" label="编号" width="230"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="100"> </el-table-column>
      <el-table-column prop="gender" label="性别" width="100">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.gender | genderFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="subjectsId.name" label="专业" width="100">
      </el-table-column>
      <el-table-column prop="classesId.name" label="班级" width="120">
      </el-table-column>
      <el-table-column prop="" label="头像" width="100">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            :size="42"
            :src="scope.row.imagePath"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-auth
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >查看</el-button
          >
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions } = createNamespacedHelpers("studentsModule");
export default {
  data() {
    return {
      // tableData: [],
      pageData: {
        currentPage: 1,
        pageSize: 5,
      },
      // total: 0,
      searchData: {
        type: "",
        value: "",
      },
    };
  },
  created() {
    this.getStudents();
  },
  watch: {
    pageData: {
      handler() {
        this.getStudents();
      },
      deep: true,
    },
  },
  // filters: {
  //   genderFilter(value) {
  //     if (value == 1 || value == "男") {
  //       return "男";
  //     } else if (value === 0 || value == "女") {
  //       return "女";
  //     }
  //     return "保密";
  //   },
  // },
  computed: {
    ...mapState(["students", "total"]),
  },
  methods: {
    ...mapActions(["getStudentsAsync"]),
    //搜索
    searchStudents() {
      // console.log(this.searchData);
      this.getStudents();
    },
    //点击编辑按钮
    handleEdit(row) {
      this.$router.push(`/home/studentUpdate/${row._id}`);
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageData.pageSize = val;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageData.currentPage = val;
    },
    //获取学生数据
    async getStudents() {
      //调用仓库模块中 action 的方法
      this.getStudentsAsync({ ...this.pageData, ...this.searchData });
      // const res = await this.$api.students.get({
      //   ...this.pageData,
      //   ...this.searchData,
      // });

      // if (res.code) {
      //   // console.log(res.data.rows);
      //   this.tableData = res.data.rows;
      //   this.total = res.data.total;
      // }
    },
    //删除学生
    async handleDelete(row) {
      const res = await this.$api.students.delete({ _id: row._id });
      if (res.code) {
        // console.log(res);
        this.$message({
          message: "删除学生成功",
          type: "success",
        });
        this.getStudents();
      } else {
        this.$message.error("删除学生失败");
      }
    },
  },
};
</script>

<style></style>
